package com.unhz.taigu.interface_controllers.tryOrder;

import com.unhz.generator.dao.ContractMapper;
import com.unhz.generator.enums.EnumTryOrder;
import com.unhz.generator.enums.EnumValid;
import com.unhz.generator.pojo.*;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.JsonResult;
import com.unhz.taigu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;

/**
 * @Author: 沈佳庆
 * @Description: 试作订单
 * @Date: Created in 下午 1:56 2018/3/2 0002
 * @Modified By: V1.0.0
 */
@RestController
@RequestMapping("/ITryOrder")
public class ITryOrderController {

    @Autowired
    private TryOrderService tryOrderService;

    @Autowired
    private TryOrderInfoService tryOrderInfoService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ReceivingInfoService receivingInfoService;

    @Autowired
    private AreaDictService areaDictService;

    @Autowired
    private UserService userService;

    @Autowired
    private ContractService contractService;
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 根据订单id获取到收货信息
     * @param orderId 订单id
     * @return
     */
    @RequestMapping("/tryToDo")
    public JsonResult tryToDo(Model model,@RequestParam(value= "id",required = false) Integer orderId){
        //根据订单查询收货相关信息
        Order order = orderService.selectOrderById(orderId);
        if(EmptyUtil.isEmpty(order)){
            return JsonResult.failure("查无此信息!");
        }
        Integer userId = order.getUserId();
        ReceivingInfo receivingInfo = receivingInfoService.selectReceivingInfo(userId);
        if(EmptyUtil.isEmpty(receivingInfo)){
            User user = userService.selectById(userId);
            model.addAttribute("user",user);
        }else{
            model.addAttribute("receivingInfo",receivingInfo);
        }
        //查询数据字典表
        List<AreaDict> areaDicts = areaDictService.selectAreaDictAll();
        //根据订单查询合同中的尺码和数量
        List<Contract> contractList = contractService.findContractsByOrderId(orderId);
        model.addAttribute("contractList",contractList);
        model.addAttribute("areaDicts",areaDicts);
        return JsonResult.success("进入进行试做页面",model);
    }

    /**
     * 修改收货信息
     * @param orderId
     * @param userName
     * @param userPhone
     * @param province
     * @param city
     * @param counties
     * @param address
     * @param adminName
     * @return
     */
    @RequestMapping("/updateInfo")
    public JsonResult updateInfo(@RequestParam(value = "orderId",required = false) Integer orderId,
                                 @RequestParam(value = "userName",required = false) String userName,
                                 @RequestParam(value = "userPhone",required = false) String userPhone,
                                 @RequestParam(value = "province",required = false) String province,
                                 @RequestParam(value = "city",required = false) String city,
                                 @RequestParam(value = "counties",required = false) String counties,
                                 @RequestParam(value = "address",required = false) String address,
                                 @RequestParam(value = "adminName",required = false) String adminName){
         Order order = orderService.selectOrderById(orderId);
        Integer userId = order.getUserId();
        List<ReceivingInfo> receivingInfoList = receivingInfoService.selectAllReceivingInfo(userId);
        String addresss = address.replaceAll(" ","");
        ReceivingInfo receivingInfos = new ReceivingInfo();
        for(ReceivingInfo receivingInfo:receivingInfoList){
            if(receivingInfo.getProvince().equals(province)
                    && receivingInfo.getCity().equals(city)
                    && receivingInfo.getCounties().equals(counties)
                    && receivingInfo.getAddress().equalsIgnoreCase(addresss)){
                Integer receivingId =  receivingInfo.getId();
                receivingInfos.setId(receivingId);
                receivingInfos.setUserId(userId);
                receivingInfos.setUpdateTime(new Date());
                receivingInfos.setCreateTime(new Date());
                receivingInfos.setProvince(province);
                receivingInfos.setCity(city);
                receivingInfos.setCounties(counties);
                receivingInfos.setAddress(address);
                receivingInfos.setUserName(userName);
                receivingInfos.setUserPhone(userPhone);
                receivingInfos.setCreateBy(adminName);
                receivingInfos.setUpdateBy(adminName);
                int count = receivingInfoService.updateReceivingInfo(receivingInfos);
                if(count>0){
                    return JsonResult.success("修改收货信息成功!");
                }
                return JsonResult.failure("修改收货信息失败!");
            }
        }
        receivingInfos.setUserId(userId);
        receivingInfos.setUpdateTime(new Date());
        receivingInfos.setCreateTime(new Date());
        receivingInfos.setProvince(province);
        receivingInfos.setCity(city);
        receivingInfos.setCounties(counties);
        receivingInfos.setAddress(address);
        receivingInfos.setUserName(userName);
        receivingInfos.setUserPhone(userPhone);
        receivingInfos.setCreateBy(adminName);
        receivingInfos.setUpdateBy(adminName);
        int count = receivingInfoService.insertAddress(receivingInfos);
        if(count>0){
            return JsonResult.success("添加收货信息成功!");
        }
        return JsonResult.failure("添加收货信息失败!");
    }

    /**
     * 添加试做订单
     * @param tryOrder
     * @param size//鞋码
     * @param number//数量
     * @return
     */
    @RequestMapping("/insertTryOrder")
    public JsonResult updateInfo(TryOrder tryOrder,
                                 @RequestParam(value = "size",required = false) String size,
                                 @RequestParam(value = "number",required = false) Integer number){
        Integer orderId = tryOrder.getOrderId();
        Order order = orderService.selectOrderById(orderId);//查询订单
        tryOrder.setTryNo(order.getOrderNo()+"A");//添加试做订单编号
        tryOrder.setApplyTime(new Date());
        tryOrder.setStatus(EnumTryOrder.APPLICATION.getIndex());
        tryOrder.setValid(EnumValid.ENABLED.getIndex());
        int count = tryOrderService.insertTryOrder(tryOrder);//添加试做订单
        Integer tryId = tryOrder.getId();
        //添加试做订单的数量
        //添加试做订单的鞋码
        String size2=size.toString();
        List<Contract> list=contractService.findContractsByOrderId(orderId);
        for (Contract contract : list) {
            TryOrderInfo tryOrderInfo = new TryOrderInfo();
            tryOrderInfo.setNumber(number);
            String contractSize=contract.getSize();
            tryOrderInfo.setTryId(tryId);
            if(size2.contains(contractSize)){
                tryOrderInfo.setSize(contractSize);
            }else{
                tryOrderInfo.setSize("0");
            }
            tryOrderInfoService.insertTryOrderInfo(tryOrderInfo);
        }

        if(count>0){
            return JsonResult.success("新增试做订单成功!");
        }
        return JsonResult.failure("新增试做订单失败!");
    }




}
